package com.hmusic.mymusic.controller;

import com.alibaba.fastjson.JSONObject;
import com.hmusic.mymusic.domain.Admin;
import com.hmusic.mymusic.domain.Singer;
import com.hmusic.mymusic.domain.Song;
import com.hmusic.mymusic.service.AdminService;
import com.hmusic.mymusic.service.SingerService;
import com.hmusic.mymusic.utils.Consts;
import com.hmusic.mymusic.utils.FileUpdateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 歌手Controller层
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/singer")
public class SingerController {

    @Autowired
    private SingerService singerService;
    @Autowired
    private FileUpdateUtil fileUpdateUtil;

    /**
     * 添加歌手
     */
    @PostMapping("/add")
    public Object addSinger(@RequestBody Singer singer){
        JSONObject jsonObject = new JSONObject();
        try{
            singerService.insert(singer);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"添加歌手成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,e.getMessage());
            return jsonObject;
        }
    }

    /**
     * 修改歌手
     */
    @PostMapping("/update")
    public Object updateSinger(@RequestBody Singer singer){
        JSONObject jsonObject = new JSONObject();
        try{
            singerService.update(singer);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"修改失败");
            return jsonObject;
        }
    }

    /**
     * 删除歌手
     */
    @GetMapping("/delete")
    @ResponseBody
    public Object deleteSinger(String id){
        JSONObject jsonObject = new JSONObject();
        try{
            singerService.delete(id);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"删除成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"删除失败");
            return jsonObject;
        }
    }

    /**
     * 根据主键查询整个对象
     */
    @GetMapping("/selectByPrimaryKey")
    @ResponseBody
    public Object selectByPrimaryKey(String id){
        return singerService.selectByPrimaryKey(id);
    }

    @GetMapping("/selectBySex")
    @ResponseBody
    public Object selectBySex(String sex){
        return singerService.singerOfSex(sex);
    }

    /**
     * 查询所有歌手
     */
    @GetMapping("/allSinger")
    @ResponseBody
    public Object allSinger(){
        return singerService.allSinger();
    }

    /**
     * 首页热门歌手
     * @return
     */
    @GetMapping("/hotSinger")
    @ResponseBody
    public Object hotSinger(){
        return singerService.hotSinger();
    }

    /**
     * 点击量增加
     * @param id
     * @return
     */
    @GetMapping("/updateClick")
    @ResponseBody
    public Object updateClick(String id){
        JSONObject jsonObject = new JSONObject();
        try{
            singerService.updateClick(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    /**
     * 上传/更新歌手图片
     */
    @PostMapping("/updateSingerPic")
    public Object updateSingerPic(@RequestParam("file") MultipartFile avatorFile,String id){
        JSONObject jsonObject = new JSONObject();
            try{
                String storeAvatorPath = fileUpdateUtil.updateSingerPic(id,avatorFile);
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPic(storeAvatorPath);
            singerService.update(singer);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"上传图片成功");
            jsonObject.put("pic",storeAvatorPath);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传图片失败");
            return jsonObject;
        }
    }

    @GetMapping("/singerCount")
    @ResponseBody
    public Object singerCount(){
        return singerService.singerCount();
    }

}
